﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using MYNAMESPACE;
using System.Net.Mail;
namespace final_project
{
    public partial class Withdrow : Form
    {
        private connect_db con;
        public string nm2;
        public string passvalue
        {

            get
            {
                return nm2;
            }
            set
            {
                nm2 = value;
            }

        }
        public string nm3;
        public string passvalue1
        {

            get
            {
                return nm3;
            }
            set
            {
                nm3 = value;
            }
        }
        public Withdrow()
        {
            InitializeComponent();
        }

        private void Withdrow_Load(object sender, EventArgs e)
        {
            //user us = new user();
            //us.setbalance(double.Parse(Balance.Text));
            //double x = us.getbalance();
            //us.setnational_id(Int32.Parse(ID.Text));
            //int y = us.getnational_id();
            ID.Text= nm3;
            Balance.Text= nm2;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 
            
            user us = new user();
           
           
            if (Value.Text.Length == 0)
            {
                this.Hide();
                MessageBox.Show("must have value");
                Account Ac2 = new Account();
                Ac2.passvalue = ID.Text;
                Ac2.ShowDialog();

            }
            else
            {
                us.setvalue(double.Parse(Value.Text));
                us.setnational_id(Int32.Parse(ID.Text));
                us.setbalance(double.Parse(Balance.Text));

                if (us.withdrwal(double.Parse(Value.Text)))
                        {


                            con = new connect_db();
                            con.sqlquery("update userdata set balance = (@namep1) where id= (@namep) ");
                            con.cmd.Parameters.Add("@namep", us.getnational_id());
                            con.cmd.Parameters.Add("@namep1",us.getbalance());
                            con.sqlnonex();
                            con.sqlquery("select balance from userdata where id=(@namep)");
                            con.cmd.Parameters.Add("@namep", ID.Text.Trim());
                            foreach (DataRow dr in con.QueryEx().Rows)
                            {

                                MessageBox.Show("the balance is " + dr[0].ToString());
                                

                            }
                            this.Hide();
                            Account Ac = new Account();
                            Ac.passvalue = ID.Text;

                            Ac.ShowDialog();
                        }
                        else
                        {
                            MessageBox.Show("do not have this value");

                        }
            
                             
         }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            user us = new user();
            us.setnational_id(Int32.Parse(ID.Text));
            this.Hide();
            Account ac = new Account();
            ac.passvalue = ID.Text;
            ac.ShowDialog();
        }

        private void Value_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Value.Text))
            {

                e.Cancel = true;
                Value.Focus();
                errorProvider1.SetError(Value, "Value Must Be Entered !");

            }
            else if (!Regex.IsMatch(Value.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Value, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Value, "");
            }

        }
    }
}
